package com.itheima.juc23.question5;

import java.util.ArrayList;
import java.util.Collections;

/**
 * @author li
 * 抽奖
 */
public class MyThread2 extends Thread {
    // 定义共享数据:{10,50,2,60,100,200,500,800,2,80,300,700}，定义集合最好
    ArrayList<Integer> list;

    public MyThread2(ArrayList<Integer> list, String name) {
        this.list = list;
        super.setName(name);
    }

    @Override
    public void run() {
        ArrayList<Integer> boxList = new ArrayList<>();
        while (true) {
            synchronized (MyThread2.class) {
                if (list.isEmpty()) {
                    System.out.println(getName() + boxList + "，最大值：" + Collections.max(boxList) + "，和：" + boxList.stream().mapToInt(Integer::intValue).sum());
                    break;
                } else {
                    // 继续抽奖
                    Collections.shuffle(list);
                    Integer prize = list.remove(0);
                    boxList.add(prize);
                }
            }
        }
    }
}
